package model;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import dao.GetCnn;

public class LoginModel {
	public static int s;
	public boolean checkLogin(String userName, String passWord) throws SQLException {
		boolean all = false;
		Connection cnn = GetCnn.getCnn();
		CallableStatement cstmt;
		cstmt = cnn.prepareCall("{call login(?,?)}",
                ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);
		cstmt.setString(1, userName);
		cstmt.registerOutParameter(2, java.sql.Types.INTEGER);
		cstmt.execute();
		s=cstmt.getInt(2);
		if(s==1){
			userName = userName.toLowerCase();
			passWord = passWord.toLowerCase();
				
			Statement stat = cnn.createStatement();
			ResultSet rst = stat.executeQuery("select* from KHACHHANG");	
			while (rst.next()) {
				String name = rst.getString("TENDANGNHAP").toLowerCase();
				String pass = rst.getString("MATKHAU").toLowerCase();
				
				if (name.equals(userName) && pass.equals(passWord)){
					all = true;
					break;
				}
				else all = false;
			}				
		}
		if(s==2){
			userName = userName.toLowerCase();
			passWord = passWord.toLowerCase();
				
			Statement stat = cnn.createStatement();
			ResultSet rst = stat.executeQuery("select* from NHANVIEN");	
			while (rst.next()) {
				String name = rst.getString("MANHANVIEN").toLowerCase();
				String pass = rst.getString("MATKHAU").toLowerCase();
				
				
				
				if (name.equals(userName) && pass.equals(passWord)) 
					{
					all = true;
					break;
					}
				else all = false;
				}				
		}
		return all;
	}
}
	
//public boolean checkLoginnv(String userName, String passWord) throws SQLException {
//		
//		userName = userName.toLowerCase();
//		passWord = passWord.toLowerCase();
//			
//		Connection cnn = GetCnn.getCnn();
//		Statement stat = cnn.createStatement();
//		ResultSet rst = stat.executeQuery("select* from NHANVIEN");	
//		while (rst.next()) {
//			String name = rst.getString("MANHANVIEN").toLowerCase();
//			String pass = rst.getString("MATKHAU").toLowerCase();
//			
//			
//			
//			if (name.equals(userName) && pass.equals(passWord)) 
//				return true;
//			}				
//		return false;
//	}
//
//}
